package com.lkm.app_ui.ui.sale.p;

import android.os.Handler;
import android.os.Looper;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.m.fun.FunModule;
import com.lkm.app_ui.m.fun.OnPayEvent;
import com.lkm.app_ui.ui.SalePrint;
import com.lkm.app_ui.ui.sale.SaleContract;
import com.lkm.frame.P;
import com.lkm.frame.otto.LinkBusCycleWrapper;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.util.CycleHelp;
import com.lkm.javatool.NumberHelp;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.help.SteelardHelp;
import com.lkm.market2_0.m.steelyarddata.OnSteelardInfoLoadEvent;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;
import com.lkm.market2_0.m.steelyardserver.event.OnSubtotalEvent;
import com.lkm.market2_0.m.steelyardserver.event.OnWeightInfoChangeEvent;
import com.lkm.market2_0.m.steelyardserver.to.WeightChangeInfo;
import com.lkm.mvp.FullLifecycleObserverA;


class SalePresenter implements SaleContract.Presenter {

    private SaleContract.View  baseView;
    private SubtotalPresenter  subtotalPresenter;
    private GoodImagePresenter goodImagePresenter;
    private Handler            mainHandler;
    private CycleHelp          cycleHelp;


    public SalePresenter(CycleHelp cycleHelp) {
        this.cycleHelp = cycleHelp;
        mainHandler = new Handler(Looper.getMainLooper());
        goodImagePresenter = new GoodImagePresenter(mainHandler, cycleHelp);
        subtotalPresenter = new SubtotalPresenter(goodImagePresenter, cycleHelp);
        goodImagePresenter.binP(subtotalPresenter);


    }


    @Override
    public void binView(SaleContract.View baseView) {
        if (baseView == null) return;

        if (this.baseView == null) {
            this.baseView = baseView;
            AppConst.mainHandler.post(() -> {
                reOnOrderAddEvent();
                reOnSteelardInfoLoadEvent();
                reOnWeightInfoChangeEvent();
            });
        }
        this.baseView = baseView;
    }


    @Override
    public SaleContract.PrintInfo createPrintInfo(OrderEntity orderEntity) {
        SteelyardInfo steelyardInfo = RunTimeData.steelyardInfo;
        String        qrCode        = com.lkm.market2_0.m_imple.steelyardserver.server.Config.createQrCode(steelyardInfo, orderEntity.orderNum);
        String        print         = new SalePrint().cretePrintContent(orderEntity, SteelardHelp.formatMoney(orderEntity.totalPrice), orderEntity.getGoods(), orderEntity.createTime, steelyardInfo, qrCode);
        return new SaleContract.PrintInfo(print, qrCode);
    }

    @Override
    public void printOrder(SaleContract.PrintInfo printInfo) {
        PrintTask.print(printInfo.print, printInfo.qrCode);
    }


    private void reOnSteelardInfoLoadEvent() {

        baseView.setPageTitle(RunTimeData.organizationInfo.getName() + " " + RunTimeData.steelyardInfo.getStallEntity().getStallName());
        LinkBusCycleWrapper.register(new ObserverAutoNext<OnSteelardInfoLoadEvent>() {
            @Override
            public Class<OnSteelardInfoLoadEvent> getBusEventClass() {
                return OnSteelardInfoLoadEvent.class;
            }

            @Override
            public void onReceiver(OnSteelardInfoLoadEvent data) {
                baseView.setPageTitle(RunTimeData.organizationInfo.getName() + " " + RunTimeData.steelyardInfo.getStallEntity().getStallName());
            }
        }, cycleHelp, CycleHelp.CycleType.Destroy);
    }


    private void reOnWeightInfoChangeEvent() {
        LinkBusCycleWrapper.register(new ObserverAutoNext<OnWeightInfoChangeEvent>() {
            @Override
            public Class<OnWeightInfoChangeEvent> getBusEventClass() {
                return OnWeightInfoChangeEvent.class;
            }

            public void postonReceiver(OnWeightInfoChangeEvent data) {
                if (baseView.isExit()) return;
                WeightChangeInfo weightChangeInfo = data.getWeightChangeInfo();

                baseView.upWeightChangeInfo(weightChangeInfo.getName(),
                        NumberHelp.formatMoney(weightChangeInfo.getWeight(), 3, "--"), SteelardHelp.formatMoney(weightChangeInfo.getUnitPrice()),
                        SteelardHelp.formatMoney(weightChangeInfo.getTotalPrice()));

            }

            @Override
            public void onReceiver(OnWeightInfoChangeEvent data) {
                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        postonReceiver(data);
                    }
                });
            }
        }, cycleHelp, CycleHelp.CycleType.Destroy);
    }


    private void reOnOrderAddEvent() {//这里应该监听着支付结果事件。
        LinkBusCycleWrapper.register(new ObserverAutoNext<OnPayEvent>() {
            @Override
            public Class<OnPayEvent> getBusEventClass() {
                return OnPayEvent.class;
            }

            @Override
            public void onReceiver(OnPayEvent data) {
                if (!data.isSuccess()) {
                    return;
                }

                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (data.getOrderEntity().type == OrderEntity.TYPE_Full1) {
                            //P.p("Bill-> SalePresenter -> onReceiver->type:" + data.getOrderEntity().type + ",isAutoPrint:" + RunTimeData.isAutoPrint);
                            SaleContract.PrintInfo printInfo = createPrintInfo(data.getOrderEntity());
                            if (RunTimeData.isAutoPrint) {
                                baseView.showTips(1);
                                PrintTask.print(printInfo.print, printInfo.qrCode);
                            }
                            baseView.showPaySuccess(printInfo);
                            subtotalPresenter.clearAll();
                        }
                    }
                });
            }
        }, cycleHelp, CycleHelp.CycleType.Destroy);
    }


    public SubtotalPresenter getSubtotalPresenter() {
        return subtotalPresenter;
    }

    public GoodImagePresenter getGoodImagePresenter() {
        return goodImagePresenter;
    }


}
